angular.module('starter.services', [])

.factory('Chats', function() {
  // Might use a resource here that returns a JSON array

  // Some fake testing data
  var chats = [{
    id: 0,
    name: 'Ben Sparrow',
    lastText: 'You on your way?',
    face: 'img/ben.png'
  }, {
    id: 1,
    name: 'Max Lynx',
    lastText: 'Hey, it\'s me',
    face: 'img/max.png'
  }, {
    id: 2,
    name: 'Adam Bradleyson',
    lastText: 'I should buy a boat',
    face: 'img/adam.jpg'
  }, {
    id: 3,
    name: 'Perry Governor',
    lastText: 'Look at my mukluks!',
    face: 'img/perry.png'
  }, {
    id: 4,
    name: 'Mike Harrington',
    lastText: 'This is wicked good ice cream.',
    face: 'img/mike.png'
  }];

  return {
    all: function() {
      return chats;
    },
    remove: function(chat) {
      chats.splice(chats.indexOf(chat), 1);
    },
    get: function(chatId) {
      for (var i = 0; i < chats.length; i++) {
        if (chats[i].id === parseInt(chatId)) {
          return chats[i];
        }
      }
      return null;
    }
  };
})
app
// .factory('jpushService',['$http','$window','$document',function($http,$window,$document){
//     var jpushServiceFactory={};

//     //var jpushapi=$window.plugins.jPushPlugin;

//     //启动极光推送
//     var _init=function(config){
//         $window.plugins.jPushPlugin.init();
//         //设置tag和Alias触发事件处理
//         document.addEventListener('jpush.setTagsWithAlias',config.stac,false);
//         //打开推送消息事件处理
//         $window.plugins.jPushPlugin.openNotificationInAndroidCallback=config.oniac;


//         $window.plugins.jPushPlugin.setDebugMode(true);
//     }
//     //获取状态
//     var _isPushStopped=function(fun){
//         $window.plugins.jPushPlugin.isPushStopped(fun)
//     }
//     //停止极光推送
//     var _stopPush=function(){
//         $window.plugins.jPushPlugin.stopPush();
//     }

//     //重启极光推送
//     var _resumePush=function(){
//         $window.plugins.jPushPlugin.resumePush();
//     }

//     //设置标签和别名
//     var _setTagsWithAlias=function(tags,alias){
//         $window.plugins.jPushPlugin.setTagsWithAlias(tags,alias);
//     }

//     //设置标签
//     var _setTags=function(tags){
//         $window.plugins.jPushPlugin.setTags(tags);
//     }

//     //设置别名
//     var _setAlias=function(alias){
//         $window.plugins.jPushPlugin.setAlias(alias);
//     }



//     jpushServiceFactory.init=_init;
//     jpushServiceFactory.isPushStopped=_isPushStopped;
//     jpushServiceFactory.stopPush=_stopPush;
//     jpushServiceFactory.resumePush=_resumePush;

//     jpushServiceFactory.setTagsWithAlias=_setTagsWithAlias;
//     jpushServiceFactory.setTags=_setTags;
//     jpushServiceFactory.setAlias=_setAlias;

//     return jpushServiceFactory;
// }])


.factory('noticeService', [function () {
    var notices=[
        {id:1,msg:'消息一'},
        {id:2,msg:'消息二'},
        {id:3,msg:'消息三'},
        {id:4,msg:'消息四'},
        {id:5,msg:'消息五'},
        {id:6,msg:'消息六'},
        {id:7,msg:'消息七'},
        {id:8,msg:'消息八'}
    ];

    return {
        notices:notices
    };
}]).factory('TokenInterceptor', function($q, $window, $location, $rootScope) {
            return {
                request: function(config) {
                    if (config.url.indexOf("up.qiniu.com") == -1) {
                        config.headers = config.headers || {};
                        if (localStorage.getItem('token')) { //$window.sessionStorage.token
                            var token ='Bearer ' + localStorage.getItem('token');
                            // console.log(token);
                            config.headers.Authorization = token; //$window.sessionStorage.token;

                        }
                    }

                    return config;
                },

                requestError: function(rejection) {
                    return $q.reject(rejection);
                },

                /* Set Authentication.isLogged to true if 200 received */
                response: function(response) {
                    if (response != null && response.status == 200 && localStorage.getItem('token')) {
                    }
                    return response || $q.when(response);
                },

                /* Revoke client authentication if 401 is received */
                responseError: function(rejection) {
                    if (rejection != null && rejection.status === 401 && (localStorage.getItem('token'))) {
                        // alert('登录过期，请重新登录');
                        // $rootScope.tologin();
                        // $window.location = '#/app/login';

                    }
                    return $q.reject(rejection);
                }
            };
        });
